{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b0d9a334",
   "metadata": {},
   "source": [
    "-----------\n",
    "\n",
    "* 时间：2022-11-20  /  week13\n",
    "* 课程：Python语言\n",
    "* 学习记录人：陈泽鑫\n",
    "\n",
    "------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5087963",
   "metadata": {},
   "source": [
    "# 一.字典\n",
    "\n",
    "## (1)P113测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e80170b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:chenzexin\n",
      "a was found 0 time(s).\n",
      "e was found 2 time(s).\n",
      "i was found 1 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "\n",
    "found = {}\n",
    "\n",
    "found['a'] = 0\n",
    "found['e'] = 0\n",
    "found['i'] = 0\n",
    "found['o'] = 0\n",
    "found['u'] = 0\n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found[letter] += 1\n",
    "for k, v in sorted(found.items()):\n",
    "    print(k, 'was found', v, 'time(s).')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc77045e",
   "metadata": {},
   "source": [
    "## (2)初始化元音的频度计数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2f7ee853",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:chenzexin\n"
     ]
    },
    {
     "ename": "KeyError",
     "evalue": "'e'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Input \u001b[1;32mIn [2]\u001b[0m, in \u001b[0;36m<cell line: 6>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m letter \u001b[38;5;129;01min\u001b[39;00m word:\n\u001b[0;32m      7\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m letter \u001b[38;5;129;01min\u001b[39;00m vowels:\n\u001b[1;32m----> 8\u001b[0m         found[letter] \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1\u001b[39m\n\u001b[0;32m      9\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m k, v \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28msorted\u001b[39m(found\u001b[38;5;241m.\u001b[39mitems()):\n\u001b[0;32m     10\u001b[0m     \u001b[38;5;28mprint\u001b[39m(k, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mwas found\u001b[39m\u001b[38;5;124m'\u001b[39m, v, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtime(s).\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
      "\u001b[1;31mKeyError\u001b[0m: 'e'"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "\n",
    "found = {}\n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found[letter] += 1\n",
    "for k, v in sorted(found.items()):\n",
    "    print(k, 'was found', v, 'time(s).')\n",
    "\n",
    "# 字典的键没有初始化，会出现“KeyError”"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "205b2f3d",
   "metadata": {},
   "source": [
    "* 避免运行时出现KeyError（用in操作符来避免在访问不存在的键时产生KeyError）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b04581dc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruits = {}\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "24b4c422",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruits['apples'] = 10\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "95c8fee1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'apples' in fruits"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26239c32",
   "metadata": {},
   "source": [
    "* 用 “in” 检查成员关系"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f9e3a81f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'bananas': 1}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if 'bananas' in fruits:\n",
    "    fruits['bananas'] += 1  # 存在，值增1\n",
    "else:\n",
    "    fruits['bananas'] = 1   # 不存在，初始化为1\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4104e685",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'bananas': 2}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if 'bananas' in fruits:\n",
    "    fruits['bananas'] += 1  \n",
    "else:\n",
    "    fruits['bananas'] = 1   \n",
    "fruits\n",
    "\n",
    "# bananas 已存在，因此值增1，变成2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "557d6039",
   "metadata": {},
   "source": [
    "* 用 “not in” 替换 “in”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f7af3030",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'bananas': 2, 'pears': 1}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if 'pears' not in fruits:\n",
    "    fruits['pears'] = 0    # 初始化为0\n",
    "fruits['pears'] += 1\n",
    "fruits"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85a7f3d2",
   "metadata": {},
   "source": [
    "* 使用 “setdefault” 方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c17e45ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'bananas': 2, 'pears': 2}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruits.setdefault('pears', 0)\n",
    "fruits['pears'] += 1\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f09f1219",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:zexin\n",
      "e was found 1 time(s).\n",
      "i was found 1 time(s).\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "\n",
    "found = {}\n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found.setdefault(letter, 0)\n",
    "        found[letter] += 1\n",
    "for k, v in sorted(found.items()):\n",
    "    print(k, 'was found', v, 'time(s).')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc013f42",
   "metadata": {},
   "source": [
    "# 二.集合\n",
    "## (1)set 函数\n",
    "* 从序列创建集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "1f5b794d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o', 'u'}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 未使用set函数前\n",
    "vowels = {'a','e','e','i','o','u','u'}\n",
    "vowels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "06dca9e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o', 'u'}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用set函数后\n",
    "vowels = set('aeeiouu')\n",
    "vowels"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4bcbf75",
   "metadata": {},
   "source": [
    "## (2)union合并集合（并集）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "29e79c07",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'h', 'i', 'l', 'o', 'u'}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "word = 'hello'\n",
    "u = vowels.union(set(word))\n",
    "u"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9bed3b7d",
   "metadata": {},
   "source": [
    "* 将集合转化为有序列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "7a7db808",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'e', 'h', 'i', 'l', 'o', 'u']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "u_list = sorted(list(u))\n",
    "u_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ab9f37a",
   "metadata": {},
   "source": [
    "## (3)difference指出共有元素（差集）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "e60dad6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'i', 'u'}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = vowels.difference(set(word))\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "b85efb91",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'h', 'l'}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = set(word).difference(vowels)\n",
    "d"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a36cf0d4",
   "metadata": {},
   "source": [
    "## (4)intersection报告共同对象（交集）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "024f090d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'e', 'o'}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i = vowels.intersection(set(word))\n",
    "i"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55ced8f6",
   "metadata": {},
   "source": [
    "## (5)P131测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "ac0bb8ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:zexin\n",
      "i\n",
      "e\n"
     ]
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = vowels.intersection(set(word))\n",
    "for vowels in found:\n",
    "    print(vowels)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e5188cc",
   "metadata": {},
   "source": [
    "# 三.元组\n",
    "## (1)元组和列表的比较"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "09a9131f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列表\n",
    "vowels1 = ['a','e','i','o','u']\n",
    "type(vowels1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "de8b97c7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 元组\n",
    "vowels2 = ('a','e','i','o','u')\n",
    "type(vowels2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7272aa51",
   "metadata": {},
   "source": [
    "## (2)元组是不可变的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "d5a42299",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'e', 'I', 'o', 'u']"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列表可变\n",
    "vowels1[2] = 'I'\n",
    "vowels1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "41811739",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [23]\u001b[0m, in \u001b[0;36m<cell line: 2>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# 元组不可变\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m vowels2[\u001b[38;5;241m2\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mI\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m      3\u001b[0m vowels2\n",
      "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "# 元组不可变\n",
    "vowels2[2] = 'I'\n",
    "vowels2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "17d05d74",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('a', 'e', 'i', 'o', 'u')"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "438aa421",
   "metadata": {},
   "source": [
    "# 三.元组在小括号内至少要包含一个逗号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "1db508eb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = ('Python',)\n",
    "type(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "4ae82a5a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Python',)"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "31ba37f0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
